package com.esri.core.tasks.geocode;

import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.util.f;
import com.esri.core.map.CallbackListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/* loaded from: classes.dex */
class LocalLocator extends Locator {
    private static final String BATCH_GEOCODING_NOT_SUPPORTED = "Batch geocoding not supported for offline locators.";
    private static final int MAX_LOCATIONS = 10;
    private static final String REVERSE_ERROR = "Unable to find address for the specified location.";
    private static final String SUGGESTIONS_NOT_SUPPORTED = "Suggestions are not supported for offline locators.";
    private static final String TRUE = "true";
    private String mFilePath;
    private long mNativePtr;

    private native long createNativeLocator(String str, boolean z, boolean z2, boolean z3, boolean z4, long j);

    private native void dispose(long j);

    private native LocatorGeocodeResult[] geocode(long j, boolean z, String[] strArr, boolean z2, boolean z3, String[] strArr2, byte[] bArr, String str, int i);

    private native String[] getAddressFields(long j);

    private native String[] getCandidateFields(long j);

    private native String[] getIntersectionCanditateFields(long j);

    private native String getServiceDescription(long j);

    private native String[] getSingleLineAddressField(long j);

    private native String getSpatialReference(long j);

    private native LocatorReverseGeocodeResult reverseGeocode(long j, double d, double d2, double d3, String str);

    static {
        f.a().initialize();
    }

    public LocalLocator(String str) {
        this.mNativePtr = 0L;
        this.mFilePath = null;
        this.mNativePtr = createNativeLocator(str, false, false, true, false, 200L);
        this.mFilePath = str;
    }

    public LocalLocator(String str, LocatorSettings locatorSettings) {
        this.mNativePtr = 0L;
        this.mFilePath = null;
        this.mNativePtr = createNativeLocator(str, true, locatorSettings.getCacheRelations(), locatorSettings.getExcludeRedundantRelations(), locatorSettings.getPreloadDictionaries(), locatorSettings.getMemoryLimit());
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public String getUrl() {
        return this.mFilePath;
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public LocatorServiceInfo getInfo() throws Exception {
        LocatorServiceInfo locatorServiceInfo = new LocatorServiceInfo();
        String[] candidateFields = getCandidateFields(this.mNativePtr);
        ArrayList arrayList = new ArrayList();
        if (candidateFields != null && candidateFields.length % 4 == 0) {
            for (int i = 0; i < candidateFields.length; i += 4) {
                LocatorFieldInfo locatorFieldInfo = new LocatorFieldInfo();
                locatorFieldInfo.setName(candidateFields[i]);
                locatorFieldInfo.setAlias(candidateFields[i + 1]);
                locatorFieldInfo.setType(candidateFields[i + 2]);
                locatorFieldInfo.setRequired(candidateFields[i + 3].equals(TRUE));
                arrayList.add(locatorFieldInfo);
            }
        }
        locatorServiceInfo.setCandidateFields(arrayList);
        ArrayList arrayList2 = new ArrayList();
        String[] addressFields = getAddressFields(this.mNativePtr);
        if (addressFields != null && addressFields.length % 4 == 0) {
            for (int i2 = 0; i2 < addressFields.length; i2 += 4) {
                LocatorFieldInfo locatorFieldInfo2 = new LocatorFieldInfo();
                locatorFieldInfo2.setName(addressFields[i2]);
                locatorFieldInfo2.setAlias(addressFields[i2 + 1]);
                locatorFieldInfo2.setType(addressFields[i2 + 2]);
                locatorFieldInfo2.setRequired(addressFields[i2 + 3].equals(TRUE));
                arrayList2.add(locatorFieldInfo2);
            }
        }
        locatorServiceInfo.setAddressFields(arrayList2);
        ArrayList arrayList3 = new ArrayList();
        String[] intersectionCanditateFields = getIntersectionCanditateFields(this.mNativePtr);
        if (intersectionCanditateFields != null && intersectionCanditateFields.length % 4 == 0) {
            for (int i3 = 0; i3 < intersectionCanditateFields.length; i3 += 4) {
                LocatorFieldInfo locatorFieldInfo3 = new LocatorFieldInfo();
                locatorFieldInfo3.setName(intersectionCanditateFields[i3]);
                locatorFieldInfo3.setAlias(intersectionCanditateFields[i3 + 1]);
                locatorFieldInfo3.setType(intersectionCanditateFields[i3 + 2]);
                locatorFieldInfo3.setRequired(intersectionCanditateFields[i3 + 3].equals(TRUE));
                arrayList3.add(locatorFieldInfo3);
            }
        }
        locatorServiceInfo.setIntersectionCandidateFields(arrayList3);
        String[] singleLineAddressField = getSingleLineAddressField(this.mNativePtr);
        if (singleLineAddressField != null && singleLineAddressField.length % 4 == 0) {
            LocatorFieldInfo locatorFieldInfo4 = new LocatorFieldInfo();
            locatorFieldInfo4.setName(singleLineAddressField[0]);
            locatorFieldInfo4.setAlias(singleLineAddressField[1]);
            locatorFieldInfo4.setType(singleLineAddressField[2]);
            locatorFieldInfo4.setRequired(singleLineAddressField[3].equals(TRUE));
            locatorServiceInfo.setSingleLineAddressField(locatorFieldInfo4);
        }
        locatorServiceInfo.setSpatialReference(SpatialReference.create(getSpatialReference(this.mNativePtr)));
        locatorServiceInfo.setServiceDescription(getServiceDescription(this.mNativePtr));
        return locatorServiceInfo;
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public List<LocatorGeocodeResult> geocode(Map<String, String> map, List<String> list) throws Exception {
        return geocode(map, list, (SpatialReference) null);
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public List<LocatorGeocodeResult> geocode(Map<String, String> map, List<String> list, SpatialReference spatialReference) throws Exception {
        LocatorGeocodeParameters locatorGeocodeParameters = new LocatorGeocodeParameters();
        locatorGeocodeParameters.setAddressFields(map);
        locatorGeocodeParameters.setOutFields(list);
        locatorGeocodeParameters.setOutSR(spatialReference);
        return geocode(locatorGeocodeParameters);
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public List<LocatorGeocodeResult> geocode(LocatorGeocodeParameters locatorGeocodeParameters) throws Exception {
        Map<String, String> addressFields = locatorGeocodeParameters.getAddressFields();
        if (addressFields == null || addressFields.isEmpty()) {
            throw new GeocodeException(Reason.REQUIRED_FIELD_MISSING);
        }
        int i = 0;
        String[] strArr = new String[addressFields.size() * 2];
        for (Map.Entry<String, String> entry : addressFields.entrySet()) {
            int i2 = i + 1;
            strArr[i] = entry.getKey();
            i = i2 + 1;
            strArr[i2] = entry.getValue();
        }
        return nativeGeocode(strArr, locatorGeocodeParameters.getOutFields(), locatorGeocodeParameters.getSearchExtent(), locatorGeocodeParameters.getInSR(), locatorGeocodeParameters.getOutSR(), 10);
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public List<LocatorGeocodeResult> find(LocatorFindParameters locatorFindParameters) throws Exception {
        String text = locatorFindParameters.getText();
        if (text == null || text.isEmpty()) {
            throw new GeocodeException(Reason.REQUIRED_FIELD_MISSING);
        }
        return nativeGeocode(new String[]{text}, locatorFindParameters.getOutFields(), locatorFindParameters.getSearchExtent(), locatorFindParameters.getInSR(), locatorFindParameters.getOutSR(), locatorFindParameters.getMaxLocations());
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public LocatorReverseGeocodeResult reverseGeocode(Point point, double d) throws Exception {
        return reverseGeocode(point, d, null, null);
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public LocatorReverseGeocodeResult reverseGeocode(Point point, double d, SpatialReference spatialReference, SpatialReference spatialReference2) throws Exception {
        if (spatialReference != null && spatialReference2 != null && !spatialReference.getText().equals(spatialReference2.getText())) {
            point = (Point) GeometryEngine.project(point, spatialReference, spatialReference2);
        }
        LocatorReverseGeocodeResult reverseGeocode = reverseGeocode(this.mNativePtr, point.getX(), point.getY(), d, spatialReference2 == null ? null : spatialReference2.getText());
        if (reverseGeocode != null) {
            return reverseGeocode;
        }
        throw new GeocodeException(REVERSE_ERROR);
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public BatchGeocodeResult batchGeocode(List<Map<String, String>> list, SpatialReference spatialReference) throws Exception {
        throw new GeocodeException(BATCH_GEOCODING_NOT_SUPPORTED);
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public List<LocatorGeocodeResult> find(LocatorSuggestionResult locatorSuggestionResult, int i, List<String> list, SpatialReference spatialReference) throws Exception {
        throw new GeocodeException(SUGGESTIONS_NOT_SUPPORTED);
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public List<LocatorSuggestionResult> suggest(LocatorSuggestionParameters locatorSuggestionParameters) throws Exception {
        throw new GeocodeException(SUGGESTIONS_NOT_SUPPORTED);
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public Future<List<LocatorSuggestionResult>> suggest(LocatorSuggestionParameters locatorSuggestionParameters, CallbackListener<List<LocatorSuggestionResult>> callbackListener) {
        throw new GeocodeException(SUGGESTIONS_NOT_SUPPORTED);
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public Future<List<LocatorGeocodeResult>> find(LocatorSuggestionResult locatorSuggestionResult, int i, List<String> list, SpatialReference spatialReference, CallbackListener<List<LocatorGeocodeResult>> callbackListener) {
        throw new GeocodeException(SUGGESTIONS_NOT_SUPPORTED);
    }

    @Override // com.esri.core.tasks.geocode.Locator
    public void dispose() {
        dispose(this.mNativePtr);
        this.mNativePtr = 0L;
    }

    protected void finalize() throws Throwable {
        dispose();
        super.finalize();
    }

    private List<LocatorGeocodeResult> nativeGeocode(String[] strArr, List<String> list, Geometry geometry, SpatialReference spatialReference, SpatialReference spatialReference2, int i) {
        String text;
        String[] strArr2;
        String[] strArr3 = list == null ? new String[0] : new String[list.size()];
        if (list != null) {
            for (int i2 = 0; i2 < strArr3.length; i2++) {
                strArr3[i2] = list.get(i2);
            }
        }
        Geometry checkAndProject = checkAndProject(geometry, spatialReference, spatialReference2);
        byte[] geometryToEsriShape = checkAndProject == null ? null : GeometryEngine.geometryToEsriShape(checkAndProject);
        if (spatialReference2 == null) {
            strArr2 = strArr;
            text = null;
        } else {
            text = spatialReference2.getText();
            strArr2 = strArr;
        }
        LocatorGeocodeResult[] geocode = geocode(this.mNativePtr, strArr2.length == 1, strArr, true, false, strArr3, geometryToEsriShape, text, i);
        return geocode == null ? new ArrayList() : new ArrayList(Arrays.asList(geocode));
    }

    private static Geometry checkAndProject(Geometry geometry, SpatialReference spatialReference, SpatialReference spatialReference2) {
        return (geometry == null || spatialReference == null || spatialReference2 == null || spatialReference.getText().equalsIgnoreCase(spatialReference2.getText())) ? geometry : GeometryEngine.project(geometry, spatialReference, spatialReference2);
    }
}
